package cn.itcast.service.impl;

import cn.itcast.dto.DishDto;
import cn.itcast.entity.Category;
import cn.itcast.entity.Dish;
import cn.itcast.entity.DishFlavor;
import cn.itcast.entity.Page;
import cn.itcast.mapper.ICategoryMapper;
import cn.itcast.mapper.IDishMapper;
import cn.itcast.mapper.ISetmealDishMapper;
import cn.itcast.service.IDishFlavorService;
import cn.itcast.service.IDishService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author GuoMingXi @Email guomingxi2018@163.com
 * @Create 2022/6/1 17:03
 * @Version V1.0
 */
@Service
public class DishServiceImpl implements IDishService {
    @Autowired(required = false)
    private IDishMapper dishMapper;
    @Autowired(required = false)
    private IDishFlavorService dishFlavorService;

    @Autowired(required = false)
    private ICategoryMapper categoryMapper;
    @Autowired
    private ISetmealDishMapper setmealDishMapper;
    /**
     * 保存数据到Dish
     * @param dishDto
     */
    @Override
    @Transactional
    @CacheEvict(value = "dishCache",allEntries = true) //添加后清除缓存
    public void save(DishDto dishDto) {
     dishMapper.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long id = dishDto.getId();
        flavors.forEach(flavor ->{
            flavor.setDishId(id);
            dishFlavorService.save(flavor);
        });
    }

    @Override
    @Cacheable(value = "pageCache:aaa",key = "#pageNum+'_'+#pageSize+'_'+#name")
    public Page<DishDto> page(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<DishDto> list = dishMapper.findByName(name);
        PageInfo<DishDto> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageNum,pageSize);
    }

    /**
     * 添加菜品信息回显
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "dishCache",key = "#id")
    public DishDto findById(Long id) {
       Dish dish =  dishMapper.findById(id);
        Long categoryId = dish.getCategoryId();
        Category category = categoryMapper.findById(categoryId);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        dishDto.setCategoryName(category.getName());
        dishDto.setFlavors(dishFlavorService.findByDishId(id));

        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    @CacheEvict(value = "dishCache",allEntries = true)
    public void update(DishDto dishDto) {
         dishMapper.update(dishDto);
        dishFlavorService.deleteByDishId(dishDto.getId());
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor ->{
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        });
    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    /**
     * 根据categoryId以及状态为1查询
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    @Cacheable(cacheNames = "dishCache",key = "#categoryId+'_'+#status")
    public List<DishDto> listFlavor(Long categoryId, Integer status) {

        return  dishMapper.findByCategoryIdAndStatus(categoryId,status);
    }


    /**
     * 通过套餐id查询对应的菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealDish(Long setmealId) {
        return setmealDishMapper.findSetmealDish(setmealId);
    }


}
